// pages/sys/yaoqing/yaoqing.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
   userInfo:null,
   hasUser: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '邀请',
    })
    console.log(app.globalData.env)
    wx.cloud.init({
      // env 参数说明：
      //   env 参数决定接下来小程序发起的云开发调用（wx.cloud.xxx）会默认请求到哪个云环境的资源
      //   此处请填入环境 ID, 环境 ID 可打开云控制台查看
      //   如不填则使用默认环境（第一个创建的环境）
       env: app.globalData.env,
     
    })
    var that= this
    wx.cloud.callFunction({
      name: 'getopenid',
      
      success: openidres => {
       
        const db = wx.cloud.database()
        //查询用户openid
        db.collection('user').where({_openid:openidres.result.openid}).get({
          success: res1 => {
            
            if(res1 && res1.data.length>0){
              if(res1.data[0].isManager > 0){
                app.globalData.isManager=res1.data[0].isManager;
                //wx.setStorageSync('isManager',  res1.data[0].isManager);
                that.setData({
                userInfo:res1.data[0],
                hasUser:true
              })
              //跳到已经邀请页面
              wx.redirectTo({
                url: 'yiyaoqing/yiyapqing',
              })
             
            }
            }else{
              
              
            }
          }
        })
      }
    })
  },
  getUserInfo: function(e) {
   debugger
   wx.getUserProfile({
     desc: 'desc',
     success:res =>{

    
      app.globalData.userInfo = res.userInfo
      this.setData({
        userInfo: e.detail.userInfo,
        hasUserInfo: true
      })
      wx.setStorageSync('userInfo', res.userInfo)
      
      this.jieshou()
  }
})
   
 
  },
  jieshou(){
    var that =this
    wx.cloud.callFunction({
      name: 'getopenid',
      
      success: openidres => {
        console.log(openidres)
        const db = wx.cloud.database()
        debugger
        db.collection('user').where({_openid:openidres.result.openid}).get({
          success: res1 => {
           console.log(res1)
            if(res1 && res1.data.length>0){
              //用户已经注册并且的普通用户
              db.collection('user').doc(res1.data[0]._id).update({
                data: {
                  isManager:2,
                  dateAdd:new Date(),
                
                },
                success: function(res) {
                  console.log(res)
                  wx.setStorageSync('isManager',  2);
                  wx.redirectTo({
                    url: 'yiyaoqing/yiyapqing',
                  })
                }
              })
            }else{
              //注册添加管理员
              let  userInfo =  app.globalData.userInfo
              debugger
              const db = wx.cloud.database()
                db.collection('user').add({
                  data: {   
                    isManager:2,
                    nickName:userInfo.nickName,
                    gender:userInfo.gender,
                    language:userInfo.language,
                    city:userInfo.city,
                    province:userInfo.province,
                    country:userInfo.country,
                    avatarUrl:userInfo.avatarUrl,
                    dateAdd:new Date()
                  },
                  success: function(res) {
                    wx.setStorageSync('isManager',  2);
                    that.setData({
                  
                    hasUser:true
                  })
                  wx.redirectTo({
                    url: 'yiyaoqing/yiyapqing',
                  })
                  }
              })
            }
           
          }
          
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
 

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var isManager = wx.getStorageSync('isManager')
    if (isManager) {
      this.setData({
        isManager: isManager,
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})